package beans;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;

@ManagedBean(name = "linhaPesquisaBean")
@RequestScoped
public class LinhaDePesquisaBean {
	private String linhaPesquisa;
	private List<String> pesquisadores;

	public LinhaDePesquisaBean() {
		pesquisadores = new ArrayList<String>();
		linhaPesquisa = (String) FacesContext.getCurrentInstance()
				.getExternalContext().getSessionMap().get("linhaPesquisa");

		String serviceURI = "http://grupo03.mooo.com/grupo03/query";
		String queryString = "prefix grupo: <http://www.owl-ontologies.com/Ontology1307822014.owl#> ";
		queryString += " SELECT ?y WHERE {";
		queryString += " ?x grupo:nome ?y.";
		queryString += " ?x <http://www.owl-ontologies.com/Ontology1307822014.owl#linhaDePesquisa> \""
				+ linhaPesquisa
				+ "\"^^<http://www.w3.org/2001/XMLSchema#string>.";
		queryString += " }";

		Query query = QueryFactory.create(queryString);

		QueryExecution qe = QueryExecutionFactory.sparqlService(serviceURI,
				query);
		ResultSet results = qe.execSelect();
		while (results.hasNext()) {
			QuerySolution solution = results.next();
			Iterator<String> iter = solution.varNames();

			while (iter.hasNext()) {
				pesquisadores.add(solution.getLiteral(iter.next()).getString());
			}

		}
		qe.close();
	}

	public String getLinhaPesquisa() {
		return linhaPesquisa;
	}

	public void setLinhaPesquisa(String linhaPesquisa) {
		this.linhaPesquisa = linhaPesquisa;
	}

	public List<String> getPesquisadores() {
		return pesquisadores;
	}

	public void setPesquisadores(List<String> pesquisadores) {
		this.pesquisadores = pesquisadores;
	}

}
